#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <arpa/inet.h>

#define SERV_IP "127.0.0.1"
#define SERV_PORT 8888

int main(int argc,char *argv[])
{
    int clientfd = socket(AF_INET, SOCK_STREAM, 0);
    if(clientfd < 0)
    {
        perror("socket");
        exit(-1);
    }

    struct sockaddr_in serveraddr;
    memset(&serveraddr, 0, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(SERV_PORT);
    serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);

    connect(clientfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr));

    char buf[256];
    while(1)
    {
        fgets(buf, sizeof(buf), stdin);
        write(clientfd, buf, strlen(buf));

        size_t nBytes = read(clientfd, buf, sizeof(buf));
        write(STDOUT_FILENO, buf, nBytes);
    }

    close(clientfd);
    return 0;
}

